<%@ page language="java" import="java.util.*,com.css.vo.*" pageEncoding="UTF-8"%>
<%@ page import="com.css.dao.*,com.css.dao.impl.*"%>
<%@ page import="com.css.dao.*,com.css.action.*"%>
<%@ page import="com.css.dao.*,com.css.service.*"%>
<%@ page import="org.springframework.context.*,org.springframework.context.support.*"%>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>修改销售信息</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
<%
    ApplicationContext myaApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    ISaleService saleService = (ISaleService) myaApplicationContext.getBean("saleService");
    ICarService carService = (ICarService) myaApplicationContext.getBean("carService");
    ICustomerService customerService = (ICustomerService) myaApplicationContext.getBean("customerService");
    IEmployeeService employeeService = (IEmployeeService) myaApplicationContext.getBean("employeeService");

    int saleId = Integer.parseInt(request.getParameter("saleId"));
    Sale sale = saleService.findById(saleId);
%>

  <header>
    <h1>修改销售信息</h1>
    <nav>
      <ul>
        <li><a href="index.jsp">首页</a></li>
        <li><a href="getList.action">系统管理</a></li>
        <li><a href="customer-management.jsp">客户信息管理</a></li>
        <li><a href="car-management.jsp">汽车信息管理</a></li>
        <li><a href="employee-management.jsp">员工信息管理</a></li>
        <li><a href="sales-management.jsp">销售信息管理</a></li>
      </ul>
    </nav>
  </header>

  <div class="content">
    <h2>修改销售信息</h2>
    <form action="updateSale.action" method="post">
      <!-- 隐藏销售ID，用于提交修改 -->
      <input type="hidden" id="sale.saleId" name="sale.saleId" value="<%= sale.getSaleId() %>">

      <!-- 客户选择 -->
      <label for="customer">客户：</label>
      <select id="sale.customerId" name="sale.customerId" required>
        <option value="">请选择客户</option>
        <% 
          List<Customer> customerList = customerService.findAllCustomer();
          for (Customer customer : customerList) {
        %>
          <option value="<%= customer.getCustomerId() %>" <%= (customer.getCustomerId().equals(sale.getCustomerId()) ? "selected" : "") %>>
            <%= customer.getCustomerName() %>
          </option>
        <% 
          }
        %>
      </select><br>

      <!-- 汽车选择 -->
      <label for="car">汽车：</label>
      <select id="sale.carId" name="sale.carId" required>
        <option value="">请选择汽车</option>
        <% 
          List<Car> carList = carService.findAllCar();
          for (Car car : carList) {
        %>
          <option value="<%= car.getCarId() %>" <%= (car.getCarId().equals(sale.getCarId()) ? "selected" : "") %>>
            <%= car.getCarModel() + " " + car.getVersion() + " " + car.getYear() + "款" + " " + car.getColor() %>
          </option>
        <% 
          }
        %>
      </select><br>

      <!-- 员工选择 -->
      <label for="employee">员工：</label>
      <select id="sale.employeeId" name="sale.employeeId" required>
        <option value="">请选择员工</option>
        <% 
          List<Employee> employeeList = employeeService.findAllEmployee();
          for (Employee employee : employeeList) {
        %>
          <option value="<%= employee.getEmployeeId() %>" <%= (employee.getEmployeeId().equals(sale.getEmployeeId()) ? "selected" : "") %>>
            <%= employee.getEmployeeName() %>
          </option>
        <% 
          }
        %>
      </select><br>

      <!-- 销售金额 -->
      <label for="saleAmount">金额：</label>
      <input type="text" id="sale.saleAmount" name="sale.saleAmount" value="<%= sale.getSaleAmount() %>" required><br>

      <!-- 订车日期 -->
      <label for="orderDate">订车日期：</label>
      <input type="date" id="sale.orderDate" name="sale.orderDate" value="<%= new SimpleDateFormat("yyyy-MM-dd").format(sale.getOrderDate()) %>" required><br>

      <!-- 交付日期 -->
      <label for="deliveryDate">交付日期：</label>
      <input type="date" id="sale.deliveryDate" name="sale.deliveryDate" value="<%= new SimpleDateFormat("yyyy-MM-dd").format(sale.getDeliveryDate()) %>" required><br>

      <!-- 提交按钮 -->
      <button type="submit">提交</button>
    </form>
  </div>
</body>
</html>
